/**
 * 
 */
package org.corpus.english.controller;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.corpus.english.model.Article;
import org.corpus.english.model.ArticleHome;
import org.corpus.english.model.Word;
import org.corpus.english.model.WordCategoryData;
import org.corpus.english.model.WordCategoryDataHome;
import org.corpus.english.model.WordHome;
import org.corpus.english.view.WordCategoryTreeNode;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treeitem;
import org.zkoss.zul.Window;


/**
 * @author robbiecheng
 *
 */
public class WordController extends GenericForwardComposer{
	private WordHome wordDao = new WordHome();
	private ArticleHome artDao = new ArticleHome();
	private WordCategoryDataHome wordCateDao = new WordCategoryDataHome();
	private Word current = new Word();
	private Article currentArticle = artDao.findById(2);
	
	public void doBeforeComposeChildren(Component comp){
		if (Executions.getCurrent().getParameter("id") != null){
			String wordId = Executions.getCurrent().getParameter("id");
			current = wordDao.findById(Integer.parseInt(wordId));
		} else if (Executions.getCurrent().getParameter("word") != null && Executions.getCurrent().getParameter("sent") != null){
			current.setWord(Executions.getCurrent().getParameter("word"));
			current.setSentences(Executions.getCurrent().getParameter("sent"));
		}
		comp.getPage().setVariable("current", current);
	}
	
	public List getAllWordCategorys(){
		return wordCateDao.findByExample(new WordCategoryData());
	}
	
	public List getAllNotEditedWords(){
		Word example = new Word();
		example.setEdited(false);
		return wordDao.findByExample(example);
	}
	
	public Word getCurrent() {
		return current;
	}
	
	public void setCurrent(Word word){
		current = word;
		//current = wordDao.findById(word.getWordid());
		//fix the over aggressive behavior of data binding
		//word.setWordCategoryDatas(current.getWordCategoryDatas());
	}
	//return article
	public Article getCurrentArticle(){
		return currentArticle;
	}
	//save article
	public void onSaveArticle$tempWin(){		
		artDao.merge(currentArticle);
		UpdateManager.showSaveSucessfulMessage();
	}
	//delete word	
	AnnotateDataBinder binder;
	public void onDeleteWord$editWin(ForwardEvent evt){
		Listbox editList = (Listbox) ((Window)evt.getOrigin().getData()).getFellow("editList");
		Set words = editList.getSelectedItems();
		for (Iterator iter = words.iterator(); iter.hasNext();) {
			wordDao.delete((Word)((Listitem)iter.next()).getValue());
			binder.loadAll();
		}
	}
	//return word count
	public int getWordCount(){
		Word example = new Word();
		example.setEdited(true);
		return wordDao.findByExample(example).size();
	}
	
	//set selected item
	public void onCreate$pos(){		
		if (pos.getSelectedIndex() >= 0)
			return;
	
		pos.setSelectedIndex(0);	
		current.setPos(pos.getSelectedItem().getLabel());		
	}
	
	
	
	//insert a new word
	private Listbox pos;
	private Tree category;
	public void onClick$save(){
		//make sure if any item is selected
		category.getSelectedItems();		
		if (current.getWordid() == 0){
			if (!current.isEdited())
				current.setEdited(true);
			//trim
			current.setWord(current.getWord() == null? null: current.getWord().trim());
			wordDao.persist(current);
		}
		else {
			if (!current.isEdited())
				current.setEdited(true);
			wordDao.merge(current);
		}
		UpdateManager.showSaveSucessfulMessage();
		
	}
	
	//save sentence category into bean
	public void onSelect$category(SelectEvent evt){
		Set selectedItems = ((Tree)evt.getTarget()).getSelectedItems();
		HashSet categorys = new HashSet(0);
		for (Iterator iterator = selectedItems.iterator(); iterator.hasNext();) {
			categorys.add(((WordCategoryTreeNode)((Treeitem)iterator.next()).getValue()).getValue());
		}
		current.setWordCategoryDatas(categorys);
	}
	
	//check redundancy
	private Button update;
	private Label wordExisted;
	private Label wordNotExited;
	public void onClick$check(){		
		Word target = new Word();
		target.setWord(current.getWord());
		target.setPos(current.getPos());
		target.setEdited(true);
		if (wordDao.findByExample(target).size() > 0){
			update.setDisabled(false);
			wordExisted.setVisible(true);
			if (wordNotExited.isVisible())
				wordNotExited.setVisible(false);
		}
		else{
			if (wordExisted.isVisible())
				wordExisted.setVisible(false);
			if (!update.isDisabled())
				update.setDisabled(true);
			wordNotExited.setVisible(true);
		}
	}
	//update a word	
	public void onClick$update(){
		Word target = new Word();
		target.setWord(current.getWord());
		target.setPos(current.getPos());
		target.setEdited(true);
		UpdateManager.updateWord(wordDao.findByExample(target).get(0).getWordid());
	}
	//clear
	public void onClick$clear(){
		UpdateManager.resetWordInput();
	}
	
	
}
